﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="BugSysSolution.JsRandom.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <link href="../Style/site.css" rel="stylesheet" />
    <script type="text/javascript" src="../Javascript/jquery-1.8.0.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <table id="tab1" class="tab">
            <tr>
                <td>Action:</td>
                <td>
                    <input type="text" tag="ActionTag" /></td>
                <td>
                    <input type="button" value="Add" onclick="conmsg.add(this);" />
                    <input type="button" value="Del" onclick="conmsg.del(this);" />
                </td>
            </tr>
        </table>
        <table id="tab2" class="tab">
            <tr>
                <td>Result:</td>
                <td>
                    <input type="text" tag="ResultTag" id="Result" style="width:300px"/></td>
                <td>
                    <input type="button" value="Action" id="btn1" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>
<script type="text/javascript">
    var ConMsg = function () {
        this.parent = $("#tab1");
        this.children = null;
        this.arr_Text = new Array();
        this.add = function () {
            this.children = $("<tr><td>Action:</td><td><input type='text' tag='ActionTag' /></td><td><input type='button' value='Add' onclick='conmsg.add(this);'/><input type='button' value='Del' onclick='conmsg.del(this);'/></td></tr>");
            this.parent.append(this.children);
        }
        this.del = function (obj) {
            var txt = $(obj).parent().prev().children("input[type=text]").val();
            if (txt == "") {
                $(obj).parent().parent().remove();
            } else {
                if (window.confirm("Are you sure delete this item?")) {
                    $(obj).parent().parent().remove();
                }
            }
        }
        this.getTextArr = function () {
            var obj = this;
            $(this.parent).find("input[type=text][tag=ActionTag]").each(function () {
                if ($(this).val() != "") {
                    obj.arr_Text.push($(this).val());
                }
            })
            if (obj.arr_Text.length == 0 || obj.arr_Text == null) {
                return null;
            }
            return obj.arr_Text;
        }
    }

    var Jsran = function () {
        this.msg = "";
        this.index = 0;
        this.init = function (arr) {
            if (arr == null || arr.length == 0) {
                this.msg = "the data source is null , please check again!";
                return false;
            }
            return true;
        }

        this.SetRandom = function (num1, num2) {
            var choices = num2 - num1 + 1;
            return Math.floor(Math.random() * choices + num1);
        }

        this.GetAction = function (arr) {
            var bl = this.init(arr);
            if (bl) {
                this.index = this.SetRandom(0, arr.length - 1);
                return arr[this.index];
            }
            return this.msg;
        }
    }

    var conmsg = new ConMsg();
    var action = new Jsran();

    $("#btn1").click(function () {
        var arr = conmsg.getTextArr();
        var msg = action.GetAction(arr);
        $("#Result").val(msg);
    })
</script>
